Public Class RUBY
#Region "Structures"
    Public Structure ResultStruct
        Dim TestCode As String
        Dim DataValue As String
        Dim Unit As String
        Dim DateTestCompleted As String
    End Structure
    Public Structure Patient
        Dim PatientName() As String
        Dim Age As String
        Dim Sex As String
        Dim DateOfBirth As String
        Dim PhysicianID As String
        Dim Location As String
        Dim PID As String
        Dim SpecimenID() As String
        Dim ResultArray() As ResultStruct
        Dim Comment As String
    End Structure
#End Region
    Private Const ComponentDelimiter As Char = "^"
    Private Const RecordDelimiter As Char = vbCr
    Private Const FrameDelimiter As Char = vbCrLf
    Private Const FieldDelimiter As Char = "|"
    Private Const RepeatDelimiter As String = "\"
    Public Const ETB As Byte = &H17
    Public Const OrderRecordType As String = "O"
    Private Const PatientRecordType As String = "P"
    Private Const ResultRecordType As String = "R"

    Public Function FrameCheck(ByVal sFrameInput As String, ByRef FinalFrame As Boolean, ByRef sDataSegmentOutput As String) As Boolean
        Dim checksum1, checksum2 As Byte
        Dim sTemp As String
        checksum1 = Convert.ToByte(sFrameInput.Substring(sFrameInput.Length - 2), 16)
        sTemp = sFrameInput.Remove(sFrameInput.Length - 2)
        sTemp = sTemp.Substring(1)
        checksum2 = StringChecksum(sTemp)
        If checksum2 <> checksum1 Then
            Return False
        Else
            sDataSegmentOutput = sTemp.Remove(sTemp.Length - 1)     'Loc bo ETX hoac ETB
            sDataSegmentOutput = sDataSegmentOutput.Substring(1)            'Loc bo Frame No.
            If Asc(Convert.ToChar(sTemp.Substring(sTemp.Length - 1))) = ETB Then
                FinalFrame = False
            Else
                FinalFrame = True
            End If
            Return True
        End If
    End Function

    Public Function GetResult(ByVal sDataSegment As String, ByRef sRecordOutput() As String) As Boolean
        Dim sTemp1 As String()
        Dim s As String
        Dim NotResult As Boolean = True
        sRecordOutput = sDataSegment.Split(RecordDelimiter)
        For Each s In sRecordOutput
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = ResultRecordType Then
                If NotResult Then
                    NotResult = False
                End If
                Exit For
            End If
        Next
        Return (Not NotResult)
    End Function
    Public Function GetTerminationStatus(ByVal sDataSegment As String) As Boolean
        Dim sRecordTemp As String()
        Dim NotEnd As Boolean = True
        Dim sTemp1 As String()
        sRecordTemp = sDataSegment.Split(RecordDelimiter)
        For Each s As String In sRecordTemp
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = "L" Then
                'If sTemp1(2).Length > 0 And sTemp1(2) = "F" Then
                NotEnd = False
                Exit For
                'End If
            End If
        Next
        Return (Not NotEnd)
    End Function

    Private Function StringChecksum(ByVal sInput As String) As Byte
        Dim bSTemp() As Char
        Dim bChecksum As Int16 = 0
        bSTemp = sInput.ToCharArray()
        For Each CharinArr As Char In bSTemp
            bChecksum += Asc(CharinArr)
            bChecksum = bChecksum Mod &H100
        Next
        Return bChecksum
    End Function

    Public Sub ResultRecordParse(ByVal sInput() As String, ByRef UniversalTestID() As String, ByRef DataValue As String, ByRef Units As String, ByRef AllergyClassRange As String, ByRef ResultAbnormalFlags As String, ByRef ResultStatus As String, ByRef DateTestCompleted As String)
        UniversalTestID = sInput(2).Split(ComponentDelimiter)
        DataValue = sInput(3)
        Units = sInput(4)
        AllergyClassRange = sInput(5)
        ResultAbnormalFlags = sInput(6)
        ResultStatus = sInput(8)
        DateTestCompleted = sInput(11)
    End Sub
    Public Sub OrderRecordParse(ByVal sInput() As String, ByRef SpecimenID() As String, ByRef UniversalTestID() As String)
        Try
            'Dim Temp2() As String
            'Lay Seq No. can xem lai neu dung SID=sInput(2)
            SpecimenID = sInput(2).Split(ComponentDelimiter)
            'Dim Temp() As String = sInput(4).Split(RepeatDelimiter)
            'ReDim UniversalTestID(21)
            'For i As Byte = 0 To Temp.GetLength(0) - 1
            '    Temp2 = Temp(i).Split(ComponentDelimiter)
            '    UniversalTestID(i) = Temp2(4)
            'Next i
        Catch ex As Exception

        End Try

    End Sub
    Public Sub PatientRecordParse(ByVal sInput() As String, ByRef PID As String, ByRef PatientName() As String, ByRef DOB As String, ByRef Sex As Char, ByRef PhysicianID As String, ByRef Location As String)
        Dim iCount As Byte = sInput.GetLength(0)
        If iCount > 2 Then
            PID = ""
        End If
        If iCount > 5 Then
            'PatientName = ""
        End If
        If iCount > 7 Then
            DOB = ""
        End If
        If iCount > 8 Then
            Sex = ""
        End If
        If iCount > 13 Then
            PhysicianID = ""
        End If
        If iCount > 25 Then
            Location = ""
        End If

    End Sub
    Public Sub ResultDataParse(ByVal sInput() As String, ByRef PatientStructure As Patient)
        Dim sTemp2() As String = {}
        Dim s() As String
        Dim NoOfTest As Byte = 0
        Dim NoOfResult As Byte = 0
        Dim Aspect As Byte = 0
        Dim UniversalTestID() As String = {}
        Dim DataValue As String = [String].Empty
        Dim Units As String = [String].Empty
        Dim AllergyClassRange As String = [String].Empty
        Dim AbnormalFlags As String = [String].Empty
        Dim ResultStatus As String = [String].Empty
        Dim DateTestCompleted As String = [String].Empty
        Dim k As Byte = 0
        With PatientStructure
            For i As Byte = 0 To sInput.GetLength(0) - 1
                s = sInput(i).Split(FieldDelimiter)
                Select Case s(0)
                    Case PatientRecordType
                        PatientRecordParse(s, .PID, .PatientName, .DateOfBirth, .Sex, .PhysicianID, .Location)
                    Case OrderRecordType
                        OrderRecordParse(s, .SpecimenID, sTemp2)
                        ReDim Preserve .ResultArray(21)
                        'For Each str As String In sTemp2
                        '    ReDim Preserve .ResultArray(NoOfTest)
                        '    .ResultArray(NoOfTest).TestCode = str
                        '    NoOfTest += 1
                        'Next
                        'NoOfTest = 16
                    Case ResultRecordType
                        ResultRecordParse(s, UniversalTestID, DataValue, Units, AllergyClassRange, AbnormalFlags, ResultStatus, DateTestCompleted)
                        If k < 22 Then
                            With .ResultArray(k)
                                .TestCode = UniversalTestID(6)
                                If DataValue.StartsWith(".") Then
                                    DataValue = "0" & DataValue
                                End If
                                If .TestCode = "HCT" Then
                                    .DataValue = DataValue / 100
                                ElseIf .TestCode = "MCHC" Then
                                    .DataValue = DataValue * 10
                                ElseIf .TestCode = "HGB" Then
                                    .DataValue = DataValue * 10
                                Else
                                    .DataValue = DataValue
                                End If
                                .Unit = Units
                                .DateTestCompleted = DateTestCompleted
                            End With
                            k = k + 1
                        End If
                        'For k As Byte = 0 To NoOfTest - 1
                        '    If .ResultArray(k).TestCode = UniversalTestID(4) Then
                        '        With .ResultArray(k)
                        '            .DataValue = DataValue
                        '            .Unit = Units
                        '            .DateTestCompleted = DateTestCompleted
                        '        End With
                        '    End If
                        'Next k
                End Select
            Next
        End With
    End Sub
End Class
